package com.pfc.wifis;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.pfc.files.*;

import com.pfc.wifis.R;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class WifiList extends Activity{

	private ArrayList<Element> nets;
	private WifiManager manWifi;
	private List<ScanResult> wifiList;
	private Element[] netsArray;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wifilist);
		String place = getIntent().getStringExtra("placeId");
		
        this.manWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        this.manWifi.startScan();
        this.wifiList = this.manWifi.getScanResults();
        
        this.nets = new ArrayList<Element>();
	    for(int i = 0; i < wifiList.size(); i++){
	    		String ssid =  wifiList.get(i).SSID;
	    		String power =  Integer.toString(wifiList.get(i).level);
	    		nets.add(new Element(ssid, power, place, "")); 
	   	}   
        
	    SavetWifis(nets, place);
	    netsArray = ArrayListToArray(nets);
        AdapterElements adapter = new AdapterElements(this);
        ListView netlist = (ListView)findViewById(R.id.listView1);
        netlist.setAdapter(adapter);
	}
	
	
	public Element[] ArrayListToArray(ArrayList<Element> list){
		Element[] e = new Element[list.size()];
		for (int i=0 ; i<list.size(); i++){
			e[i] = list.get(i);
		}
		return e;
	}
	

    private void SavetWifis(ArrayList<Element> nets, String place){
    		if (Environment.getExternalStorageState().equals("mounted")) {
    			File sdCard = Environment.getExternalStorageDirectory();
    			FileManager fileM = new FileManager(sdCard.getAbsolutePath(), place);
    			//fileM.WriteFile(nets);
    		}
    		else{
    			Toast.makeText(getBaseContext(),"El almacenamineto externio no se encuentra disponible",
						Toast.LENGTH_LONG).show();
    		}

    }
	
    
    class AdapterElements extends ArrayAdapter<Object> {
		Activity context;
		
		public AdapterElements(Activity context) {
			
    		super(context, R.layout.elementitems, netsArray);
    		this.context = context;
		}
		
		
		
    	public View getView(int position, View convertView, ViewGroup parent) {
		    
			LayoutInflater inflater = context.getLayoutInflater();
			View item = inflater.inflate(R.layout.elementitems, null);
			
			//Carga SSID.
			TextView lblSsid = (TextView)item.findViewById(R.id.strssid);
			lblSsid.setText(nets.get(position).getSsid());
			//Carga level.
			TextView lblLevel = (TextView)item.findViewById(R.id.strlevel);
			lblLevel.setText(nets.get(position).getLevel());
			
			return(item);
		}
    }
}
